package com.sanrenzu.chunbo.ui.foodagency;
import android.os.Handler;
import android.os.Looper;
import com.sanrenzu.chunbo.bean.gy.CateGoryListBean;
import com.sanrenzu.chunbo.bean.gy.ChefListBean;
import com.sanrenzu.chunbo.bean.gy.FoodAgencyBean;
import com.sanrenzu.chunbo.bean.gy.ListFocusBean;
import com.sanrenzu.chunbo.bean.gy.NewFoodBean;
import com.sanrenzu.chunbo.bean.gy.NewFoodTypeBean;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/8/30 0030.
 */
public class FoodAgencyPresenter implements FoodAgencyContract.Presenter {
    private FoodAgencyContract.View view;
    private FoodAgencyContract.Model model;
    public FoodAgencyPresenter(FoodAgencyContract.View view){
        this.view=view;
        model=new FoodAgencyModel();
    }
    //表明我要做的事（网络数据访问），然后通过modle获取网络数据， 通过view确定我是否展现成功，展现了json数据
    @Override
    public void getData() {
        model.getData(new Callback<FoodAgencyBean>() {
            @Override
            public void onResponse(Call<FoodAgencyBean> call, Response<FoodAgencyBean> response) {
                //获取食谱社的对象
                FoodAgencyBean bean=response.body();
                //获取食谱分类的集合
                final List<CateGoryListBean>list=bean.getCategory().getList();
                final List<ChefListBean> chefListBeen=bean.getChef().getList();
                final List<ListFocusBean> listFocus = bean.getListFocus();
                //通过handler传递给主线程数据
                Handler handler=new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //调用加载成功后的方法
                    view.onGetDataSuccess(list,listFocus,chefListBeen);
                    }
                });
            }
            @Override
            public void onFailure(Call<FoodAgencyBean> call, Throwable t) {
                Handler handler=new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetDataFail("网络数据获取失败");
                    }
                });
            }
        });
    }

    @Override
    public void getNewFoodData() {
        model.getNewFoodData(new Callback<NewFoodBean>() {
            @Override
            public void onResponse(Call<NewFoodBean> call, Response<NewFoodBean> response) {
                NewFoodBean newFoodBean=response.body();
                final List<NewFoodTypeBean> list=newFoodBean.getList();
                Handler handler=new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetNewFoodDataSuccess(list);
                    }
                });
            }
            @Override
            public void onFailure(Call<NewFoodBean> call, Throwable t) {
                Handler handler=new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetNewFoodDataFail("网络数据获取失败");
                    }
                });
            }
        });
    }
}
